home *** CD-ROM | disk | FTP | other *** search
/ 8bitfiles.net/archives / archives.tar / archives / compuserve-file-archive / 22 Graphics & Utilities / GIF89M.TXT < prev    next >
Encoding:
Text File  |  2019-04-13  |  81.7 KB  |  2,481 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.     Cover Sheet for the GIF89a Specification
  7.  
  8.  
  9.     DEFERRED CLEAR CODE IN LZW COMPRESSION
  10.  
  11.     There has been confusion about where clear codes can be found in the
  12.     data stream.  As the specification says, they may appear at anytime.  There
  13.     is not a requirement to send a clear code when the string table is full.
  14.  
  15.     It is the encoder's decision as to when the table should be cleared.  When
  16.     the table is full, the encoder can chose to use the table as is, making no
  17.     changes to it until the encoder chooses to clear it.  The encoder during
  18.     this time sends out codes that are of the maximum Code Size.
  19.  
  20.     As we can see from the above, when the decoder's table is full, it must
  21.     not change the table until a clear code is received.  The Code Size is that
  22.     of the maximum Code Size.  Processing other than this is done normally.
  23.  
  24.     Because of a large base of decoders that do not handle the decompression in
  25.     this manner, we ask developers of GIF encoding software to NOT implement
  26.     this feature until at least January 1991 and later if they see that their
  27.     particular market is not ready for it.  This will give developers of GIF
  28.     decoding software time to implement this feature and to get it into the
  29.     hands of their clients before the decoders start "breaking" on the new
  30.     GIF's.  It is not required that encoders change their software to take
  31.     advantage of the deferred clear code, but it is for decoders.
  32.  
  33.     APPLICATION EXTENSION BLOCK - APPLICATION IDENTIFIER
  34.  
  35.     There will be a Courtesy Directory file located on CompuServe in the PICS
  36.     forum.  This directory will contain Application Identifiers for Application
  37.     Extension Blocks that have been used by developers of GIF applications.
  38.     This file is intended to help keep developers that wish to create
  39.     Application Extension Blocks from using the same Application Identifiers.
  40.     This is not an official directory; it is for voluntary participation only
  41.     and does not guarantee that someone will not use the same identifier.
  42.  
  43.     E-Mail can be sent to Larry Wood (forum manager of PICS) indicating the
  44.     request for inclusion in this file with an identifier.
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.                         GRAPHICS INTERCHANGE FORMAT(sm)
  84.  
  85.                             Version 89a (modified)
  86.  
  87.                             (c)1987,1988,1989,1990
  88.  
  89.                                    Copyright
  90.                             CompuServe Incorporated
  91.                                 Columbus, Ohio
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122. CompuServe Incorporated                           Graphics Interchange Format
  123. Document Date : 9 January, 1995                         Programming Reference
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.                                Table of Contents
  134.  
  135. Disclaimer.................................................................  1
  136.  
  137. Foreword...................................................................  1
  138.  
  139. Licensing..................................................................  1
  140.  
  141. About the Document.........................................................  2
  142.  
  143. General Description........................................................  2
  144.  
  145. Version Numbers............................................................  2
  146.  
  147. The Encoder................................................................  3
  148.  
  149. The Decoder................................................................  3
  150.  
  151. Compliance.................................................................  3
  152.  
  153. About Recommendations......................................................  4
  154.  
  155. About Color Tables.........................................................  4
  156.  
  157. Blocks, Extensions and Scope...............................................  4
  158.  
  159. Block Sizes................................................................  5
  160.  
  161. Using GIF as an embedded protocol..........................................  5
  162.  
  163. Data Sub-blocks............................................................  5
  164.  
  165. Block Terminator...........................................................  6
  166.  
  167. Header.....................................................................  7
  168.  
  169. Logical Screen Descriptor..................................................  8
  170.  
  171. Global Color Table......................................................... 10
  172.  
  173. Image Descriptor........................................................... 11
  174.  
  175. Local Color Table.......................................................... 13
  176.  
  177. Table Based Image Data..................................................... 14
  178.  
  179. Graphic Control Extension.................................................. 15
  180.  
  181. Comment Extension.......................................................... 17
  182.  
  183. Plain Text Extension....................................................... 18
  184.  
  185. Application Extension...................................................... 21
  186.  
  187. Trailer.................................................................... 23
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198. Quick Reference Table...................................................... 24
  199.  
  200. GIF Grammar................................................................ 25
  201.  
  202. Glossary................................................................... 27
  203.  
  204. Conventions................................................................ 28
  205.  
  206. Interlaced Images.......................................................... 29
  207.  
  208. Variable-Length-Code LZW Compression....................................... 30
  209.  
  210. On-line Capabilities Dialogue.............................................. 33
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.                                                                          1
  261.  
  262.  
  263. 1. Disclaimer.
  264.  
  265. The information provided herein is subject to change without notice. In no
  266. event will CompuServe Incorporated be liable for damages, including any loss of
  267. revenue, loss of profits or other incidental or consequential damages arising
  268. out of the use or inability to use the information; CompuServe Incorporated
  269. makes no claim as to the suitability of the information.
  270.  
  271.  
  272. 2. Foreword.
  273.  
  274. This document defines the Graphics Interchange Format(sm). The specification
  275. given here defines version 89a, which is an extension of version 87a.
  276.  
  277. The Graphics Interchange Format(sm) as specified here should be considered
  278. complete; any deviation from it should be considered invalid, including but not
  279. limited to, the use of reserved or undefined fields within control or data
  280. blocks, the inclusion of extraneous data within or between blocks, the use of
  281. methods or algorithms not specifically listed as part of the format, etc. In
  282. general, any and all deviations, extensions or modifications not specified in
  283. this document should be considered to be in violation of the format and should
  284. be avoided.
  285.  
  286.  
  287. 3. Licensing.
  288.  
  289. The Graphics Interchange Format(c) is the copyright property of CompuServe
  290. Incorporated. Only CompuServe Incorporated is authorized to define, redefine,
  291. enhance, alter, modify or change in any way the definition of the format.
  292.  
  293. CompuServe Incorporated hereby grants a limited, non-exclusive, royalty-free
  294. license for the use of the Graphics Interchange Format(sm) in computer
  295. software; computer software utilizing GIF(sm) must acknowledge ownership of the
  296. Graphics Interchange Format and its Service Mark by CompuServe Incorporated, in
  297. User and Technical Documentation. Computer software utilizing GIF, which is
  298. distributed or may be distributed without User or Technical Documentation must
  299. display to the screen or printer a message acknowledging ownership of the
  300. Graphics Interchange Format and the Service Mark by CompuServe Incorporated; in
  301. this case, the acknowledgement may be displayed in an opening screen or leading
  302. banner, or a closing screen or trailing banner. A message such as the following
  303. may be used:
  304.  
  305.       "The Graphics Interchange Format(c) is the Copyright property of
  306.       CompuServe Incorporated. GIF(sm) is a Service Mark property of
  307.       CompuServe Incorporated."
  308.  
  309. For further information, please contact :
  310.  
  311.       CompuServe Incorporated
  312.       Graphics Technology Department
  313.       5000 Arlington Center Boulevard
  314.       Columbus, Ohio  43220
  315.       U. S. A.
  316. CompuServe Incorporated maintains a mailing list with all those individuals and
  317. organizations who wish to receive copies of this document when it is corrected
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.                                                                          2
  326.  
  327.  
  328. or revised. This service is offered free of charge; please provide us with your
  329. mailing address.
  330.  
  331.  
  332. Users of this specification should note that the LZW compression and
  333. decompression methods described in U.S. Patent No. 4,558,302 and certain
  334. corresponding foreign patents are owned by Unisys Corporation.  Software and
  335. hardware developers may be required to obtain a license under this patent in
  336. order to develop and market products using GIF LZW compression and
  337. decompression.  Unisys has agreed that developers may obtain such a license on
  338. reasonable, non-discriminatory terms and conditions.  Further information may
  339. be obtained from: Welch Licensing Department, Office of the General Counsel, M/S
  340. C1SW19, Unisys Corporation, Blue Bell, PA 19424.
  341.  
  342.  
  343. 4. About the Document.
  344.  
  345. This document describes in detail the definition of the Graphics Interchange
  346. Format.  This document is intended as a programming reference; it is
  347. recommended that the entire document be read carefully before programming,
  348. because of the interdependence of the various parts. There is an individual
  349. section for each of the Format blocks. Within each section, the sub-section
  350. labeled Required Version refers to the version number that an encoder will have
  351. to use if the corresponding block is used in the Data Stream. Within each
  352. section, a diagram describes the individual fields in the block; the diagrams
  353. are drawn vertically; top bytes in the diagram appear first in the Data Stream.
  354. Bits within a byte are drawn most significant on the left end.  Multi-byte
  355. numeric fields are ordered Least Significant Byte first. Numeric constants are
  356. represented as Hexadecimal numbers, preceded by "0x".  Bit fields within a byte
  357. are described in order from most significant bits to least significant bits.
  358.  
  359.  
  360. 5. General Description.
  361.  
  362. The Graphics Interchange Format(sm) defines a protocol intended for the on-line
  363. transmission and interchange of raster graphic data in a way that is
  364. independent of the hardware used in their creation or display.
  365.  
  366. The Graphics Interchange Format is defined in terms of blocks and sub-blocks
  367. which contain relevant parameters and data used in the reproduction of a
  368. graphic. A GIF Data Stream is a sequence of protocol blocks and sub-blocks
  369. representing a collection of graphics. In general, the graphics in a Data
  370. Stream are assumed to be related to some degree, and to share some control
  371. information; it is recommended that encoders attempt to group together related
  372. graphics in order to minimize hardware changes during processing and to
  373. minimize control information overhead. For the same reason, unrelated graphics
  374. or graphics which require resetting hardware parameters should be encoded
  375. separately to the extent possible.
  376.  
  377. A Data Stream may originate locally, as when read from a file, or it may
  378. originate remotely, as when transmitted over a data communications line. The
  379. Format is defined with the assumption that an error-free Transport Level
  380. Protocol is used for communications; the Format makes no provisions for
  381. error-detection and error-correction.
  382.  
  383. The GIF Data Stream must be interpreted in context, that is, the application
  384. program must rely on information external to the Data Stream to invoke the
  385. decoder process.
  386.  
  387.  
  388. 6. Version Numbers.
  389.  
  390. The version number in the Header of a Data Stream is intended to identify the
  391. minimum set of capabilities required of a decoder in order to fully process the
  392. Data Stream.  An encoder should use the earliest possible version number that
  393. includes all the blocks used in the Data Stream. Within each block section in
  394. this document, there is an entry labeled Required Version which specifies the
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402.                                                                          3
  403.  
  404.  
  405. earliest version number that includes the corresponding block.  The encoder
  406. should make every attempt to use the earliest version number covering all the
  407. blocks in the Data Stream; the unnecessary use of later version numbers will
  408. hinder processing by some decoders.
  409.  
  410.  
  411. 7. The Encoder.
  412.  
  413. The Encoder is the program used to create a GIF Data Stream. From raster data
  414. and other information, the encoder produces the necessary control and data
  415. blocks needed for reproducing the original graphics.
  416.  
  417. The encoder has the following primary responsibilities.
  418.  
  419.             - Include in the Data Stream all the necessary information to
  420.             reproduce  the graphics.
  421.  
  422.             - Insure that a Data Stream is labeled with the earliest possible
  423.             Version Number that will cover the definition of all the blocks in
  424.             it; this is to ensure that the largest number of decoders can
  425.             process the Data Stream.
  426.  
  427.             - Ensure encoding of the graphics in such a way that the decoding
  428.             process is optimized. Avoid redundant information as much as
  429.             possible.
  430.  
  431.             - To the extent possible, avoid grouping graphics which might
  432.             require resetting hardware parameters during the decoding process.
  433.  
  434.             - Set to zero (off) each of the bits of each and every field
  435.             designated as reserved. Note that some fields in the Logical Screen
  436.             Descriptor and the Image Descriptor were reserved under Version
  437.             87a, but are used under version 89a.
  438.  
  439.  
  440. 8. The Decoder.
  441.  
  442. The Decoder is the program used to process a GIF Data Stream. It processes the
  443. Data Stream sequentially, parsing the various blocks and sub-blocks, using the
  444. control information to set hardware and process parameters and interpreting the
  445. data to render the graphics.
  446.  
  447. The decoder has the following primary responsibilities.
  448.  
  449.             - Process each graphic in the Data Stream in sequence, without
  450.             delays other than those specified in the control information.
  451.  
  452.             - Set its hardware parameters to fit, as closely as possible, the
  453.             control information contained in the Data Stream.
  454.  
  455.  
  456. 9. Compliance.
  457.  
  458. An encoder or a decoder is said to comply with a given version of the Graphics
  459. Interchange Format if and only if it fully conforms with and correctly
  460. implements the definition of the standard associated with that version.  An
  461.  
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468.                                                                          4
  469.  
  470.  
  471. encoder or a decoder may be compliant with a given version number and not
  472. compliant with some subsequent version.
  473.  
  474.  
  475. 10. About Recommendations.
  476.  
  477. Each block section in this document contains an entry labeled Recommendation;
  478. this section lists a set of recommendations intended to guide and organize the
  479. use of the particular blocks. Such recommendations are geared towards making
  480. the functions of encoders and decoders more efficient, as well as making
  481. optimal use of the communications bandwidth.  It is advised that these
  482. recommendations be followed.
  483.  
  484.  
  485. 11. About Color Tables.
  486.  
  487. The GIF format utilizes color tables to render raster-based graphics. A color
  488. table can have one of two different scopes: global or local. A Global Color
  489. Table is used by all those graphics in the Data Stream which do not have a
  490. Local Color Table associated with them. The scope of the Global Color Table is
  491. the entire Data Stream. A Local Color Table is always associated with the
  492. graphic that immediately follows it; the scope of a Local Color Table is
  493. limited to that single graphic. A Local Color Table supersedes a Global Color
  494. Table, that is, if a Data Stream contains a Global Color Table, and an image
  495. has a Local Color Table associated with it, the decoder must save the Global
  496. Color Table, use the Local Color Table to render the image, and then restore
  497. the Global Color Table. Both types of color tables are optional, making it
  498. possible for a Data Stream to contain numerous graphics without a color table
  499. at all. For this reason, it is recommended that the decoder save the last
  500. Global Color Table used until another Global Color Table is encountered. In
  501. this way, a Data Stream which does not contain either a Global Color Table or
  502. a Local Color Table may be processed using the last Global Color Table saved.
  503. If a Global Color Table from a previous Stream is used, that table becomes the
  504. Global Color Table of the present Stream. This is intended to reduce the
  505. overhead incurred by color tables. In particular, it is recommended that an
  506. encoder use only one Global Color Table if all the images in related Data
  507. Streams can be rendered with the same table.  If no color table is available at
  508. all, the decoder is free to use a system color table or a table of its own. In
  509. that case, the decoder may use a color table with as many colors as its
  510. hardware is able to support; it is recommended that such a table have black and
  511. white as its first two entries, so that monochrome images can be rendered
  512. adequately.
  513.  
  514. The Definition of the GIF Format allows for a Data Stream to contain only the
  515. Header, the Logical Screen Descriptor, a Global Color Table and the GIF
  516. Trailer. Such a Data Stream would be used to load a decoder with a Global Color
  517. Table, in preparation for subsequent Data Streams without a color table at all.
  518.  
  519.  
  520. 12. Blocks, Extensions and Scope.
  521.  
  522. Blocks can be classified into three groups : Control, Graphic-Rendering and
  523. Special Purpose.  Control blocks, such as the Header, the Logical Screen
  524. Descriptor, the Graphic Control Extension and the Trailer, contain information
  525. used to control the process of the Data Stream or information  used in setting
  526. hardware parameters.  Graphic-Rendering blocks such as the Image Descriptor and
  527.  
  528.  
  529.  
  530.  
  531.  
  532.  
  533.  
  534.                                                                          5
  535.  
  536.  
  537. the Plain Text Extension contain information and data used to render a graphic
  538. on the display device. Special Purpose blocks such as the Comment Extension and
  539. the Application Extension are neither used to control the process of the Data
  540. Stream nor do they contain information or data used to render a graphic on the
  541. display device. With the exception of the Logical Screen Descriptor and the
  542. Global Color Table, whose scope is the entire Data Stream, all other Control
  543. blocks have a limited scope, restricted to the Graphic-Rendering block that
  544. follows them.  Special Purpose blocks do not delimit the scope of any Control
  545. blocks; Special Purpose blocks are transparent to the decoding process.
  546. Graphic-Rendering blocks and extensions are used as scope delimiters for
  547. Control blocks and extensions. The labels used to identify labeled blocks fall
  548. into three ranges : 0x00-0x7F (0-127) are the Graphic Rendering blocks,
  549. excluding the Trailer (0x3B); 0x80-0xF9 (128-249) are the Control blocks;
  550. 0xFA-0xFF (250-255) are the Special Purpose blocks. These ranges are defined so
  551. that decoders can handle block scope by appropriately identifying block labels,
  552. even when the block itself cannot be processed.
  553.  
  554.  
  555. 13. Block Sizes.
  556.  
  557. The Block Size field in a block, counts the number of bytes remaining in the
  558. block, not counting the Block Size field itself, and not counting the Block
  559. Terminator, if one is to follow.  Blocks other than Data Blocks are intended to
  560. be of fixed length; the Block Size field is provided in order to facilitate
  561. skipping them, not to allow their size to change in the future.  Data blocks
  562. and sub-blocks are of variable length to accommodate the amount of data.
  563.  
  564.  
  565. 14. Using GIF as an embedded protocol.
  566.  
  567. As an embedded protocol, GIF may be part of larger application protocols,
  568. within which GIF is used to render graphics.  In such a case, the application
  569. protocol could define a block within which the GIF Data Stream would be
  570. contained. The application program would then invoke a GIF decoder upon
  571. encountering a block of type GIF.  This approach is recommended in favor of
  572. using Application Extensions, which become overhead for all other applications
  573. that do not process them. Because a GIF Data Stream must be processed in
  574. context, the application must rely on some means of identifying the GIF Data
  575. Stream outside of the Stream itself.
  576.  
  577.  
  578. 15. Data Sub-blocks.
  579.  
  580.       a. Description. Data Sub-blocks are units containing data. They do not
  581.       have a label, these blocks are processed in the context of control
  582.       blocks, wherever data blocks are specified in the format. The first byte
  583.       of the Data sub-block indicates the number of data bytes to follow. A
  584.       data sub-block may contain from 0 to 255 data bytes. The size of the
  585.       block does not account for the size byte itself, therefore, the empty
  586.       sub-block is one whose size field contains 0x00.
  587.  
  588.       b. Required Version.  87a.
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.  
  599.  
  600.                                                                          6
  601.  
  602.  
  603.       c. Syntax.
  604.  
  605.       7 6 5 4 3 2 1 0        Field Name                    Type
  606.      +---------------+
  607.   0  |               |       Block Size                    Byte
  608.      +---------------+
  609.   1  |               |
  610.      +-             -+
  611.   2  |               |
  612.      +-             -+
  613.   3  |               |
  614.      +-             -+
  615.      |               |       Data Values                   Byte
  616.      +-             -+
  617.  up  |               |
  618.      +-   . . . .   -+
  619.  to  |               |
  620.      +-             -+
  621.      |               |
  622.      +-             -+
  623. 255  |               |
  624.      +---------------+
  625.  
  626.             i) Block Size - Number of bytes in the Data Sub-block; the size
  627.             must be within 0 and 255 bytes, inclusive.
  628.  
  629.             ii) Data Values - Any 8-bit value. There must be exactly as many
  630.             Data Values as specified by the Block Size field.
  631.  
  632.       d. Extensions and Scope. This type of block always occurs as part of a
  633.       larger unit. It does not have a scope of itself.
  634.  
  635.       e. Recommendation. None.
  636.  
  637.  
  638. 16. Block Terminator.
  639.  
  640.       a. Description. This zero-length Data Sub-block is used to terminate a
  641.       sequence of Data Sub-blocks. It contains a single byte in the position of
  642.       the Block Size field and does not contain data.
  643.  
  644.       b. Required Version.  87a.
  645.  
  646.       c. Syntax.
  647.  
  648.       7 6 5 4 3 2 1 0        Field Name                    Type
  649.      +---------------+
  650.   0  |               |       Block Size                    Byte
  651.      +---------------+
  652.  
  653.             i) Block Size - Number of bytes in the Data Sub-block; this field
  654.             contains the fixed value 0x00.
  655.  
  656.             ii) Data Values - This block does not contain any data.
  657.  
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664.  
  665.  
  666.                                                                          7
  667.  
  668.  
  669.       d. Extensions and Scope. This block terminates the immediately preceding
  670.       sequence of Data Sub-blocks. This block cannot be modified by any
  671.       extension.
  672.  
  673.       e. Recommendation. None.
  674.  
  675.  
  676. 17. Header.
  677.  
  678.       a. Description. The Header identifies the GIF Data Stream in context. The
  679.       Signature field marks the beginning of the Data Stream, and the Version
  680.       field identifies the set of capabilities required of a decoder to fully
  681.       process the Data Stream.  This block is REQUIRED; exactly one Header must
  682.       be present per Data Stream.
  683.  
  684.       b. Required Version.  Not applicable. This block is not subject to a
  685.       version number. This block must appear at the beginning of every Data
  686.       Stream.
  687.  
  688.       c. Syntax.
  689.  
  690.  
  691.       7 6 5 4 3 2 1 0        Field Name                    Type
  692.      +---------------+
  693.    0 |               |       Signature                     3 Bytes
  694.      +-             -+
  695.    1 |               |
  696.      +-             -+
  697.    2 |               |
  698.      +---------------+
  699.    3 |               |       Version                       3 Bytes
  700.      +-             -+
  701.    4 |               |
  702.      +-             -+
  703.    5 |               |
  704.      +---------------+
  705.  
  706.             i) Signature - Identifies the GIF Data Stream. This field contains
  707.             the fixed value 'GIF'.
  708.  
  709.             ii) Version - Version number used to format the data stream.
  710.             Identifies the minimum set of capabilities necessary to a decoder
  711.             to fully process the contents of the Data Stream.
  712.  
  713.             Version Numbers as of 10 July 1990 :       "87a" - May 1987
  714.                                                        "89a" - July 1989
  715.  
  716.             Version numbers are ordered numerically increasing on the first two
  717.             digits starting with 87 (87,88,...,99,00,...,85,86) and
  718.             alphabetically increasing on the third character (a,...,z).
  719.  
  720.             iii) Extensions and Scope. The scope of this block is the entire
  721.             Data Stream. This block cannot be modified by any extension.
  722.  
  723.  
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730.  
  731.  
  732.                                                                          8
  733.  
  734.  
  735.       d. Recommendations.
  736.  
  737.             i) Signature - This field identifies the beginning of the GIF Data
  738.             Stream; it is not intended to provide a unique signature for the
  739.             identification of the data. It is recommended that the GIF Data
  740.             Stream be identified externally by the application. (Refer to
  741.             Appendix G for on-line identification of the GIF Data Stream.)
  742.  
  743.             ii) Version - ENCODER : An encoder should use the earliest possible
  744.             version number that defines all the blocks used in the Data Stream.
  745.             When two or more Data Streams are combined, the latest of the
  746.             individual version numbers should be used for the resulting Data
  747.             Stream. DECODER : A decoder should attempt to process the data
  748.             stream to the best of its ability; if it encounters a version
  749.             number which it is not capable of processing fully, it should
  750.             nevertheless, attempt to process the data stream to the best of its
  751.             ability, perhaps after warning the user that the data may be
  752.             incomplete.
  753.  
  754.  
  755. 18. Logical Screen Descriptor.
  756.  
  757.       a. Description.  The Logical Screen Descriptor contains the parameters
  758.       necessary to define the area of the display device within which the
  759.       images will be rendered.  The coordinates in this block are given with
  760.       respect to the top-left corner of the virtual screen; they do not
  761.       necessarily refer to absolute coordinates on the display device.  This
  762.       implies that they could refer to window coordinates in a window-based
  763.       environment or printer coordinates when a printer is used.
  764.  
  765.       This block is REQUIRED; exactly one Logical Screen Descriptor must be
  766.       present per Data Stream.
  767.  
  768.       b. Required Version.  Not applicable. This block is not subject to a
  769.       version number. This block must appear immediately after the Header.
  770.  
  771.       c. Syntax.
  772.  
  773.       7 6 5 4 3 2 1 0        Field Name                    Type
  774.      +---------------+
  775.   0  |               |       Logical Screen Width          Unsigned
  776.      +-             -+
  777.   1  |               |
  778.      +---------------+
  779.   2  |               |       Logical Screen Height         Unsigned
  780.      +-             -+
  781.   3  |               |
  782.      +---------------+
  783.   4  | |     | |     |       <Packed Fields>               See below
  784.      +---------------+
  785.   5  |               |       Background Color Index        Byte
  786.      +---------------+
  787.   6  |               |       Pixel Aspect Ratio            Byte
  788.      +---------------+
  789.  
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796.  
  797.  
  798.                                                                          9
  799.  
  800.  
  801.      <Packed Fields>  =      Global Color Table Flag       1 Bit
  802.                              Color Resolution              3 Bits
  803.                              Sort Flag                     1 Bit
  804.                              Size of Global Color Table    3 Bits
  805.  
  806.             i) Logical Screen Width - Width, in pixels, of the Logical Screen
  807.             where the images will be rendered in the displaying device.
  808.  
  809.             ii) Logical Screen Height - Height, in pixels, of the Logical
  810.             Screen where the images will be rendered in the displaying device.
  811.  
  812.             iii) Global Color Table Flag - Flag indicating the presence of a
  813.             Global Color Table; if the flag is set, the Global Color Table will
  814.             immediately follow the Logical Screen Descriptor. This flag also
  815.             selects the interpretation of the Background Color Index; if the
  816.             flag is set, the value of the Background Color Index field should
  817.             be used as the table index of the background color. (This field is
  818.             the most significant bit of the byte.)
  819.  
  820.             Values :    0 -   No Global Color Table follows, the Background
  821.                               Color Index field is meaningless.
  822.                         1 -   A Global Color Table will immediately follow, the
  823.                               Background Color Index field is meaningful.
  824.  
  825.             iv) Color Resolution - Number of bits per primary color available
  826.             to the original image, minus 1. This value represents the size of
  827.             the entire palette from which the colors in the graphic were
  828.             selected, not the number of colors actually used in the graphic.
  829.             For example, if the value in this field is 3, then the palette of
  830.             the original image had 4 bits per primary color available to create
  831.             the image.  This value should be set to indicate the richness of
  832.             the original palette, even if not every color from the whole
  833.             palette is available on the source machine.
  834.  
  835.             v) Sort Flag - Indicates whether the Global Color Table is sorted.
  836.             If the flag is set, the Global Color Table is sorted, in order of
  837.             decreasing importance. Typically, the order would be decreasing
  838.             frequency, with most frequent color first. This assists a decoder,
  839.             with fewer available colors, in choosing the best subset of colors;
  840.             the decoder may use an initial segment of the table to render the
  841.             graphic.
  842.  
  843.             Values :    0 -   Not ordered.
  844.                         1 -   Ordered by decreasing importance, most
  845.                               important color first.
  846.  
  847.             vi) Size of Global Color Table - If the Global Color Table Flag is
  848.             set to 1, the value in this field is used to calculate the number
  849.             of bytes contained in the Global Color Table. To determine that
  850.             actual size of the color table, raise 2 to [the value of the field
  851.             + 1].  Even if there is no Global Color Table specified, set this
  852.             field according to the above formula so that decoders can choose
  853.             the best graphics mode to display the stream in.  (This field is
  854.             made up of the 3 least significant bits of the byte.)
  855.  
  856.             vii) Background Color Index - Index into the Global Color Table for
  857.  
  858.  
  859.  
  860.  
  861.  
  862.  
  863.  
  864.                                                                         10
  865.  
  866.  
  867.             the Background Color. The Background Color is the color used for
  868.             those pixels on the screen that are not covered by an image. If the
  869.             Global Color Table Flag is set to (zero), this field should be zero
  870.             and should be ignored.
  871.  
  872.             viii) Pixel Aspect Ratio - Factor used to compute an approximation
  873.             of the aspect ratio of the pixel in the original image.  If the
  874.             value of the field is not 0, this approximation of the aspect ratio
  875.             is computed based on the formula:
  876.  
  877.             Aspect Ratio = (Pixel Aspect Ratio + 15) / 64
  878.  
  879.             The Pixel Aspect Ratio is defined to be the quotient of the pixel's
  880.             width over its height.  The value range in this field allows
  881.             specification of the widest pixel of 4:1 to the tallest pixel of
  882.             1:4 in increments of 1/64th.
  883.  
  884.             Values :        0 -   No aspect ratio information is given.
  885.                        1..255 -   Value used in the computation.
  886.  
  887.       d. Extensions and Scope. The scope of this block is the entire Data
  888.       Stream. This block cannot be modified by any extension.
  889.  
  890.       e. Recommendations. None.
  891.  
  892.  
  893. 19. Global Color Table.
  894.  
  895.       a. Description. This block contains a color table, which is a sequence of
  896.       bytes representing red-green-blue color triplets. The Global Color Table
  897.       is used by images without a Local Color Table and by Plain Text
  898.       Extensions. Its presence is marked by the Global Color Table Flag being
  899.       set to 1 in the Logical Screen Descriptor; if present, it immediately
  900.       follows the Logical Screen Descriptor and contains a number of bytes
  901.       equal to
  902.                     3 x 2^(Size of Global Color Table+1).
  903.  
  904.       This block is OPTIONAL; at most one Global Color Table may be present
  905.       per Data Stream.
  906.  
  907.       b. Required Version.  87a
  908.  
  909.  
  910.  
  911.  
  912.  
  913.  
  914.  
  915.  
  916.  
  917.  
  918.  
  919.  
  920.  
  921.  
  922.  
  923.  
  924.  
  925.  
  926.  
  927.  
  928.  
  929.  
  930.                                                                         11
  931.  
  932.  
  933.       c. Syntax.
  934.  
  935.       7 6 5 4 3 2 1 0        Field Name                    Type
  936.      +===============+
  937.   0  |               |       Red 0                         Byte
  938.      +-             -+
  939.   1  |               |       Green 0                       Byte
  940.      +-             -+
  941.   2  |               |       Blue 0                        Byte
  942.      +-             -+
  943.   3  |               |       Red 1                         Byte
  944.      +-             -+
  945.      |               |       Green 1                       Byte
  946.      +-             -+
  947.  up  |               |
  948.      +-   . . . .   -+       ...
  949.  to  |               |
  950.      +-             -+
  951.      |               |       Green 255                     Byte
  952.      +-             -+
  953. 767  |               |       Blue 255                      Byte
  954.      +===============+
  955.  
  956.  
  957.       d. Extensions and Scope. The scope of this block is the entire Data
  958.       Stream. This block cannot be modified by any extension.
  959.  
  960.       e. Recommendation. None.
  961.  
  962.  
  963. 20. Image Descriptor.
  964.  
  965.       a. Description. Each image in the Data Stream is composed of an Image
  966.       Descriptor, an optional Local Color Table, and the image data.  Each
  967.       image must fit within the boundaries of the Logical Screen, as defined
  968.       in the Logical Screen Descriptor.
  969.  
  970.       The Image Descriptor contains the parameters necessary to process a table
  971.       based image. The coordinates given in this block refer to coordinates
  972.       within the Logical Screen, and are given in pixels. This block is a
  973.       Graphic-Rendering Block, optionally preceded by one or more Control
  974.       blocks such as the Graphic Control Extension, and may be optionally
  975.       followed by a Local Color Table; the Image Descriptor is always followed
  976.       by the image data.
  977.  
  978.       This block is REQUIRED for an image.  Exactly one Image Descriptor must
  979.       be present per image in the Data Stream.  An unlimited number of images
  980.       may be present per Data Stream.
  981.  
  982.       b. Required Version.  87a.
  983.  
  984.  
  985.  
  986.  
  987.  
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994.  
  995.  
  996.                                                                         12
  997.  
  998.  
  999.       c. Syntax.
  1000.  
  1001.       7 6 5 4 3 2 1 0        Field Name                    Type
  1002.      +---------------+
  1003.   0  |               |       Image Separator               Byte
  1004.      +---------------+
  1005.   1  |               |       Image Left Position           Unsigned
  1006.      +-             -+
  1007.   2  |               |
  1008.      +---------------+
  1009.   3  |               |       Image Top Position            Unsigned
  1010.      +-             -+
  1011.   4  |               |
  1012.      +---------------+
  1013.   5  |               |       Image Width                   Unsigned
  1014.      +-             -+
  1015.   6  |               |
  1016.      +---------------+
  1017.   7  |               |       Image Height                  Unsigned
  1018.      +-             -+
  1019.   8  |               |
  1020.      +---------------+
  1021.   9  | | | |   |     |       <Packed Fields>               See below
  1022.      +---------------+
  1023.  
  1024.      <Packed Fields>  =      Local Color Table Flag        1 Bit
  1025.                              Interlace Flag                1 Bit
  1026.                              Sort Flag                     1 Bit
  1027.                              Reserved                      2 Bits
  1028.                              Size of Local Color Table     3 Bits
  1029.  
  1030.            i) Image Separator - Identifies the beginning of an Image
  1031.            Descriptor. This field contains the fixed value 0x2C.
  1032.  
  1033.            ii) Image Left Position - Column number, in pixels, of the left edge
  1034.            of the image, with respect to the left edge of the Logical Screen.
  1035.            Leftmost column of the Logical Screen is 0.
  1036.  
  1037.            iii) Image Top Position - Row number, in pixels, of the top edge of
  1038.            the image with respect to the top edge of the Logical Screen. Top
  1039.            row of the Logical Screen is 0.
  1040.  
  1041.            iv) Image Width - Width of the image in pixels.
  1042.  
  1043.            v) Image Height - Height of the image in pixels.
  1044.  
  1045.            vi) Local Color Table Flag - Indicates the presence of a Local Color
  1046.            Table immediately following this Image Descriptor. (This field is
  1047.            the most significant bit of the byte.)
  1048.  
  1049.  
  1050.            Values :    0 -   Local Color Table is not present. Use
  1051.                              Global Color Table if available.
  1052.                        1 -   Local Color Table present, and to follow
  1053.                              immediately after this Image Descriptor.
  1054.  
  1055.  
  1056.  
  1057.  
  1058.  
  1059.  
  1060.  
  1061.  
  1062.                                                                         13
  1063.  
  1064.  
  1065.            vii) Interlace Flag - Indicates if the image is interlaced. An image
  1066.            is interlaced in a four-pass interlace pattern; see Appendix E for
  1067.            details.
  1068.  
  1069.            Values :    0 - Image is not interlaced.
  1070.                        1 - Image is interlaced.
  1071.  
  1072.             viii) Sort Flag - Indicates whether the Local Color Table is
  1073.             sorted.  If the flag is set, the Local Color Table is sorted, in
  1074.             order of decreasing importance. Typically, the order would be
  1075.             decreasing frequency, with most frequent color first. This assists
  1076.             a decoder, with fewer available colors, in choosing the best subset
  1077.             of colors; the decoder may use an initial segment of the table to
  1078.             render the graphic.
  1079.  
  1080.             Values :    0 -   Not ordered.
  1081.                         1 -   Ordered by decreasing importance, most
  1082.                               important color first.
  1083.  
  1084.             ix) Size of Local Color Table - If the Local Color Table Flag is
  1085.             set to 1, the value in this field is used to calculate the number
  1086.             of bytes contained in the Local Color Table. To determine that
  1087.             actual size of the color table, raise 2 to the value of the field
  1088.             + 1. This value should be 0 if there is no Local Color Table
  1089.             specified. (This field is made up of the 3 least significant bits
  1090.             of the byte.)
  1091.  
  1092.      d. Extensions and Scope. The scope of this block is the Table-based Image
  1093.      Data Block that follows it. This block may be modified by the Graphic
  1094.      Control Extension.
  1095.  
  1096.      e. Recommendation. None.
  1097.  
  1098.  
  1099. 21. Local Color Table.
  1100.  
  1101.      a. Description. This block contains a color table, which is a sequence of
  1102.      bytes representing red-green-blue color triplets. The Local Color Table
  1103.      is used by the image that immediately follows. Its presence is marked by
  1104.      the Local Color Table Flag being set to 1 in the Image Descriptor; if
  1105.      present, the Local Color Table immediately follows the Image Descriptor
  1106.      and contains a number of bytes equal to
  1107.                           3x2^(Size of Local Color Table+1).
  1108.      If present, this color table temporarily becomes the active color table
  1109.      and the following image should be processed using it. This block is
  1110.      OPTIONAL; at most one Local Color Table may be present per Image
  1111.      Descriptor and its scope is the single image associated with the Image
  1112.      Descriptor that precedes it.
  1113.  
  1114.      b. Required Version.  87a.
  1115.  
  1116.  
  1117.  
  1118.  
  1119.  
  1120.  
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126.  
  1127.  
  1128.                                                                         14
  1129.  
  1130.  
  1131.      c. Syntax.
  1132.  
  1133.       7 6 5 4 3 2 1 0        Field Name                    Type
  1134.      +===============+
  1135.   0  |               |       Red 0                         Byte
  1136.      +-             -+
  1137.   1  |               |       Green 0                       Byte
  1138.      +-             -+
  1139.   2  |               |       Blue 0                        Byte
  1140.      +-             -+
  1141.   3  |               |       Red 1                         Byte
  1142.      +-             -+
  1143.      |               |       Green 1                       Byte
  1144.      +-             -+
  1145.  up  |               |
  1146.      +-   . . . .   -+       ...
  1147.  to  |               |
  1148.      +-             -+
  1149.      |               |       Green 255                     Byte
  1150.      +-             -+
  1151. 767  |               |       Blue 255                      Byte
  1152.      +===============+
  1153.  
  1154.  
  1155.      d. Extensions and Scope. The scope of this block is the Table-based Image
  1156.      Data Block that immediately follows it. This block cannot be modified by
  1157.      any extension.
  1158.  
  1159.      e. Recommendations. None.
  1160.  
  1161.  
  1162. 22. Table Based Image Data.
  1163.  
  1164.      a. Description. The image data for a table based image consists of a
  1165.      sequence of sub-blocks, of size at most 255 bytes each, containing an
  1166.      index into the active color table, for each pixel in the image.  Pixel
  1167.      indices are in order of left to right and from top to bottom.  Each index
  1168.      must be within the range of the size of the active color table, starting
  1169.      at 0. The sequence of indices is encoded using the LZW Algorithm with
  1170.      variable-length code, as described in Appendix F
  1171.  
  1172.      b. Required Version.  87a.
  1173.  
  1174.      c. Syntax. The image data format is as follows:
  1175.  
  1176.       7 6 5 4 3 2 1 0        Field Name                    Type
  1177.      +---------------+
  1178.      |               |       LZW Minimum Code Size         Byte
  1179.      +---------------+
  1180.  
  1181.      +===============+
  1182.      |               |
  1183.      /               /       Image Data                    Data Sub-blocks
  1184.      |               |
  1185.      +===============+
  1186.  
  1187.  
  1188.  
  1189.  
  1190.  
  1191.  
  1192.  
  1193.  
  1194.                                                                         15
  1195.  
  1196.  
  1197.             i) LZW Minimum Code Size.  This byte determines the initial number
  1198.             of bits used for LZW codes in the image data, as described in
  1199.             Appendix F.
  1200.  
  1201.      d. Extensions and Scope. This block has no scope, it contains raster
  1202.      data. Extensions intended to modify a Table-based image must appear
  1203.      before the corresponding Image Descriptor.
  1204.  
  1205.      e. Recommendations. None.
  1206.  
  1207.  
  1208. 23. Graphic Control Extension.
  1209.  
  1210.       a. Description. The Graphic Control Extension contains parameters used
  1211.       when processing a graphic rendering block. The scope of this extension is
  1212.       the first graphic rendering block to follow. The extension contains only
  1213.       one data sub-block.
  1214.  
  1215.       This block is OPTIONAL; at most one Graphic Control Extension may precede
  1216.       a graphic rendering block. This is the only limit to the number of
  1217.       Graphic Control Extensions that may be contained in a Data Stream.
  1218.  
  1219.       b. Required Version.  89a.
  1220.  
  1221.       c. Syntax.
  1222.  
  1223.       7 6 5 4 3 2 1 0        Field Name                    Type
  1224.      +---------------+
  1225.   0  |               |       Extension Introducer          Byte
  1226.      +---------------+
  1227.   1  |               |       Graphic Control Label         Byte
  1228.      +---------------+
  1229.  
  1230.      +---------------+
  1231.   0  |               |       Block Size                    Byte
  1232.      +---------------+
  1233.   1  |     |     | | |       <Packed Fields>               See below
  1234.      +---------------+
  1235.   2  |               |       Delay Time                    Unsigned
  1236.      +-             -+
  1237.   3  |               |
  1238.      +---------------+
  1239.   4  |               |       Transparent Color Index       Byte
  1240.      +---------------+
  1241.  
  1242.      +---------------+
  1243.   0  |               |       Block Terminator              Byte
  1244.      +---------------+
  1245.  
  1246.  
  1247.       <Packed Fields>  =     Reserved                      3 Bits
  1248.                              Disposal Method               3 Bits
  1249.                              User Input Flag               1 Bit
  1250.                              Transparent Color Flag        1 Bit
  1251.  
  1252.             i) Extension Introducer - Identifies the beginning of an extension
  1253.  
  1254.  
  1255.  
  1256.  
  1257.  
  1258.  
  1259.  
  1260.                                                                         16
  1261.  
  1262.  
  1263.             block. This field contains the fixed value 0x21.
  1264.  
  1265.             ii) Graphic Control Label - Identifies the current block as a
  1266.             Graphic Control Extension. This field contains the fixed value
  1267.             0xF9.
  1268.  
  1269.             iii) Block Size - Number of bytes in the block, after the Block
  1270.             Size field and up to but not including the Block Terminator.  This
  1271.             field contains the fixed value 4.
  1272.  
  1273.             iv) Disposal Method - Indicates the way in which the graphic is to
  1274.             be treated after being displayed.
  1275.  
  1276.             Values :    0 -   No disposal specified. The decoder is
  1277.                               not required to take any action.
  1278.                         1 -   Do not dispose. The graphic is to be left
  1279.                               in place.
  1280.                         2 -   Restore to background color. The area used by the
  1281.                               graphic must be restored to the background color.
  1282.                         3 -   Restore to previous. The decoder is required to
  1283.                               restore the area overwritten by the graphic with
  1284.                               what was there prior to rendering the graphic.
  1285.                      4-7 -    To be defined.
  1286.  
  1287.             v) User Input Flag - Indicates whether or not user input is
  1288.             expected before continuing. If the flag is set, processing will
  1289.             continue when user input is entered. The nature of the User input
  1290.             is determined by the application (Carriage Return, Mouse Button
  1291.             Click, etc.).
  1292.  
  1293.             Values :    0 -   User input is not expected.
  1294.                         1 -   User input is expected.
  1295.  
  1296.             When a Delay Time is used and the User Input Flag is set,
  1297.             processing will continue when user input is received or when the
  1298.             delay time expires, whichever occurs first.
  1299.  
  1300.             vi) Transparency Flag - Indicates whether a transparency index is
  1301.             given in the Transparent Index field. (This field is the least
  1302.             significant bit of the byte.)
  1303.  
  1304.             Values :    0 -   Transparent Index is not given.
  1305.                         1 -   Transparent Index is given.
  1306.  
  1307.             vii) Delay Time - If not 0, this field specifies the number of
  1308.             hundredths (1/100) of a second to wait before continuing with the
  1309.             processing of the Data Stream. The clock starts ticking immediately
  1310.             after the graphic is rendered. This field may be used in
  1311.             conjunction with the User Input Flag field.
  1312.  
  1313.             viii) Transparency Index - The Transparency Index is such that when
  1314.             encountered, the corresponding pixel of the display device is not
  1315.             modified and processing goes on to the next pixel. The index is
  1316.             present if and only if the Transparency Flag is set to 1.
  1317.  
  1318.             ix) Block Terminator - This zero-length data block marks the end of
  1319.  
  1320.  
  1321.  
  1322.  
  1323.  
  1324.  
  1325.  
  1326.                                                                         17
  1327.  
  1328.             the Graphic Control Extension.
  1329.  
  1330.       d. Extensions and Scope. The scope of this Extension is the graphic
  1331.       rendering block that follows it; it is possible for other extensions to
  1332.       be present between this block and its target. This block can modify the
  1333.       Image Descriptor Block and the Plain Text Extension.
  1334.  
  1335.       e. Recommendations.
  1336.  
  1337.             i) Disposal Method - The mode Restore To Previous is intended to be
  1338.             used in small sections of the graphic; the use of this mode imposes
  1339.             severe demands on the decoder to store the section of the graphic
  1340.             that needs to be saved. For this reason, this mode should be used
  1341.             sparingly.  This mode is not intended to save an entire graphic or
  1342.             large areas of a graphic; when this is the case, the encoder should
  1343.             make every attempt to make the sections of the graphic to be
  1344.             restored be separate graphics in the data stream. In the case where
  1345.             a decoder is not capable of saving an area of a graphic marked as
  1346.             Restore To Previous, it is recommended that a decoder restore to
  1347.             the background color.
  1348.  
  1349.             ii) User Input Flag - When the flag is set, indicating that user
  1350.             input is expected, the decoder may sound the bell (0x07) to alert
  1351.             the user that input is being expected.  In the absence of a
  1352.             specified Delay Time, the decoder should wait for user input
  1353.             indefinitely.  It is recommended that the encoder not set the User
  1354.             Input Flag without a Delay Time specified.
  1355.  
  1356.  
  1357. 24. Comment Extension.
  1358.  
  1359.       a. Description. The Comment Extension contains textual information which
  1360.       is not part of the actual graphics in the GIF Data Stream. It is suitable
  1361.       for including comments about the graphics, credits, descriptions or any
  1362.       other type of non-control and non-graphic data.  The Comment Extension
  1363.       may be ignored by the decoder, or it may be saved for later processing;
  1364.       under no circumstances should a Comment Extension disrupt or interfere
  1365.       with the processing of the Data Stream.
  1366.  
  1367.       This block is OPTIONAL; any number of them may appear in the Data Stream.
  1368.  
  1369.       b. Required Version.  89a.
  1370.  
  1371.  
  1372.  
  1373.  
  1374.  
  1375.  
  1376.  
  1377.  
  1378.  
  1379.  
  1380.  
  1381.  
  1382.  
  1383.  
  1384.  
  1385.  
  1386.  
  1387.  
  1388.  
  1389.  
  1390.  
  1391.  
  1392.                                                                         18
  1393.  
  1394.  
  1395.       c. Syntax.
  1396.  
  1397.       7 6 5 4 3 2 1 0        Field Name                    Type
  1398.      +---------------+
  1399.   0  |               |       Extension Introducer          Byte
  1400.      +---------------+
  1401.   1  |               |       Comment Label                 Byte
  1402.      +---------------+
  1403.  
  1404.      +===============+
  1405.      |               |
  1406.   N  |               |       Comment Data                  Data Sub-blocks
  1407.      |               |
  1408.      +===============+
  1409.  
  1410.      +---------------+
  1411.   0  |               |       Block Terminator              Byte
  1412.      +---------------+
  1413.  
  1414.             i) Extension Introducer - Identifies the beginning of an extension
  1415.             block. This field contains the fixed value 0x21.
  1416.  
  1417.             ii) Comment Label - Identifies the block as a Comment Extension.
  1418.             This field contains the fixed value 0xFE.
  1419.  
  1420.             iii) Comment Data - Sequence of sub-blocks, each of size at most
  1421.             255 bytes and at least 1 byte, with the size in a byte preceding
  1422.             the data.  The end of the sequence is marked by the Block
  1423.             Terminator.
  1424.  
  1425.             iv) Block Terminator - This zero-length data block marks the end of
  1426.             the Comment Extension.
  1427.  
  1428.       d. Extensions and Scope. This block does not have scope. This block
  1429.       cannot be modified by any extension.
  1430.  
  1431.       e. Recommendations.
  1432.  
  1433.             i) Data - This block is intended for humans.  It should contain
  1434.             text using the 7-bit ASCII character set. This block should
  1435.             not be used to store control information for custom processing.
  1436.  
  1437.             ii) Position - This block may appear at any point in the Data
  1438.             Stream at which a block can begin; however, it is recommended that
  1439.             Comment Extensions do not interfere with Control or Data blocks;
  1440.             they should be located at the beginning or at the end of the Data
  1441.             Stream to the extent possible.
  1442.  
  1443.  
  1444. 25. Plain Text Extension.
  1445.  
  1446.       a. Description. The Plain Text Extension contains textual data and the
  1447.       parameters necessary to render that data as a graphic, in a simple form.
  1448.       The textual data will be encoded with the 7-bit printable ASCII
  1449.       characters.  Text data are rendered using a grid of character cells
  1450.  
  1451.  
  1452.  
  1453.  
  1454.  
  1455.  
  1456.  
  1457.  
  1458.                                                                         19
  1459.  
  1460.  
  1461.       defined by the parameters in the block fields. Each character is rendered
  1462.       in an individual cell. The textual data in this block is to be rendered
  1463.       as mono-spaced characters, one character per cell, with a best fitting
  1464.       font and size. For further information, see the section on
  1465.       Recommendations below. The data characters are taken sequentially from
  1466.       the data portion of the block and rendered within a cell, starting with
  1467.       the upper left cell in the grid and proceeding from left to right and
  1468.       from top to bottom. Text data is rendered until the end of data is
  1469.       reached or the character grid is filled.  The Character Grid contains an
  1470.       integral number of cells; in the case that the cell dimensions do not
  1471.       allow for an integral number, fractional cells must be discarded; an
  1472.       encoder must be careful to specify the grid dimensions accurately so that
  1473.       this does not happen. This block requires a Global Color Table to be
  1474.       available; the colors used by this block reference the Global Color Table
  1475.       in the Stream if there is one, or the Global Color Table from a previous
  1476.       Stream, if one was saved. This block is a graphic rendering block,
  1477.       therefore it may be modified by a Graphic Control Extension.  This block
  1478.       is OPTIONAL; any number of them may appear in the Data Stream.
  1479.  
  1480.       b. Required Version.  89a.
  1481.  
  1482.  
  1483.  
  1484.  
  1485.  
  1486.  
  1487.  
  1488.  
  1489.  
  1490.  
  1491.  
  1492.  
  1493.  
  1494.  
  1495.  
  1496.  
  1497.  
  1498.  
  1499.  
  1500.  
  1501.  
  1502.  
  1503.  
  1504.  
  1505.  
  1506.  
  1507.  
  1508.  
  1509.  
  1510.  
  1511.  
  1512.  
  1513.  
  1514.  
  1515.  
  1516.  
  1517.  
  1518.  
  1519.  
  1520.  
  1521.  
  1522.  
  1523.  
  1524.                                                                         20
  1525.  
  1526.  
  1527.       c. Syntax.
  1528.  
  1529.       7 6 5 4 3 2 1 0        Field Name                    Type
  1530.      +---------------+
  1531.   0  |               |       Extension Introducer          Byte
  1532.      +---------------+
  1533.   1  |               |       Plain Text Label              Byte
  1534.      +---------------+
  1535.  
  1536.      +---------------+
  1537.   0  |               |       Block Size                    Byte
  1538.      +---------------+
  1539.   1  |               |       Text Grid Left Position       Unsigned
  1540.      +-             -+
  1541.   2  |               |
  1542.      +---------------+
  1543.   3  |               |       Text Grid Top Position        Unsigned
  1544.      +-             -+
  1545.   4  |               |
  1546.      +---------------+
  1547.   5  |               |       Text Grid Width               Unsigned
  1548.      +-             -+
  1549.   6  |               |
  1550.      +---------------+
  1551.   7  |               |       Text Grid Height              Unsigned
  1552.      +-             -+
  1553.   8  |               |
  1554.      +---------------+
  1555.   9  |               |       Character Cell Width          Byte
  1556.      +---------------+
  1557.  10  |               |       Character Cell Height         Byte
  1558.      +---------------+
  1559.  11  |               |       Text Foreground Color Index   Byte
  1560.      +---------------+
  1561.  12  |               |       Text Background Color Index   Byte
  1562.      +---------------+
  1563.  
  1564.      +===============+
  1565.      |               |
  1566.   N  |               |       Plain Text Data               Data Sub-blocks
  1567.      |               |
  1568.      +===============+
  1569.  
  1570.      +---------------+
  1571.   0  |               |       Block Terminator              Byte
  1572.      +---------------+
  1573.  
  1574.             i) Extension Introducer - Identifies the beginning of an extension
  1575.             block. This field contains the fixed value 0x21.
  1576.  
  1577.             ii) Plain Text Label - Identifies the current block as a Plain Text
  1578.             Extension. This field contains the fixed value 0x01.
  1579.  
  1580.             iii) Block Size - Number of bytes in the extension, after the Block
  1581.             Size field and up to but not including the beginning of the data
  1582.             portion. This field contains the fixed value 12.
  1583.  
  1584.  
  1585.  
  1586.  
  1587.  
  1588.  
  1589.  
  1590.                                                                         21
  1591.  
  1592.  
  1593.             iv) Text Grid Left Position - Column number, in pixels, of the left
  1594.             edge of the text grid, with respect to the left edge of the Logical
  1595.             Screen.
  1596.  
  1597.             v) Text Grid Top Position - Row number, in pixels, of the top edge
  1598.             of the text grid, with respect to the top edge of the Logical
  1599.             Screen.
  1600.  
  1601.             vi) Image Grid Width - Width of the text grid in pixels.
  1602.  
  1603.             vii) Image Grid Height - Height of the text grid in pixels.
  1604.  
  1605.             viii) Character Cell Width - Width, in pixels, of each cell in the
  1606.             grid.
  1607.  
  1608.             ix) Character Cell Height - Height, in pixels, of each cell in the
  1609.             grid.
  1610.  
  1611.             x) Text Foreground Color Index - Index into the Global Color Table
  1612.             to be used to render the text foreground.
  1613.  
  1614.             xi) Text Background Color Index - Index into the Global Color Table
  1615.             to be used to render the text background.
  1616.  
  1617.             xii) Plain Text Data - Sequence of sub-blocks, each of size at most
  1618.             255 bytes and at least 1 byte, with the size in a byte preceding
  1619.             the data.  The end of the sequence is marked by the Block
  1620.             Terminator.
  1621.  
  1622.             xiii) Block Terminator - This zero-length data block marks the end
  1623.             of the Plain Text Data Blocks.
  1624.  
  1625.       d. Extensions and Scope. The scope of this block is the Plain Text Data
  1626.       Block contained in it. This block may be modified by the Graphic Control
  1627.       Extension.
  1628.  
  1629.       e. Recommendations. The data in the Plain Text Extension is assumed to be
  1630.       preformatted. The selection of font and size is left to the discretion of
  1631.       the decoder.  If characters less than 0x20 or greater than 0xf7 are
  1632.       encountered, it is recommended that the decoder display a Space character
  1633.       (0x20). The encoder should use grid and cell dimensions such that an
  1634.       integral number of cells fit in the grid both horizontally as well as
  1635.       vertically.  For broadest compatibility, character cell dimensions should
  1636.       be around 8x8 or 8x16 (width x height); consider an image for unusual
  1637.       sized text.
  1638.  
  1639.  
  1640. 26. Application Extension.
  1641.  
  1642.       a. Description. The Application Extension contains application-specific
  1643.       information; it conforms with the extension block syntax, as described
  1644.       below, and its block label is 0xFF.
  1645.  
  1646.       b. Required Version.  89a.
  1647.  
  1648.  
  1649.  
  1650.  
  1651.  
  1652.  
  1653.  
  1654.  
  1655.  
  1656.                                                                         22
  1657.  
  1658.  
  1659.       c. Syntax.
  1660.  
  1661.       7 6 5 4 3 2 1 0        Field Name                    Type
  1662.      +---------------+
  1663.   0  |               |       Extension Introducer          Byte
  1664.      +---------------+
  1665.   1  |               |       Extension Label               Byte
  1666.      +---------------+
  1667.  
  1668.      +---------------+
  1669.   0  |               |       Block Size                    Byte
  1670.      +---------------+
  1671.   1  |               |
  1672.      +-             -+
  1673.   2  |               |
  1674.      +-             -+
  1675.   3  |               |       Application Identifier        8 Bytes
  1676.      +-             -+
  1677.   4  |               |
  1678.      +-             -+
  1679.   5  |               |
  1680.      +-             -+
  1681.   6  |               |
  1682.      +-             -+
  1683.   7  |               |
  1684.      +-             -+
  1685.   8  |               |
  1686.      +---------------+
  1687.   9  |               |
  1688.      +-             -+
  1689.  10  |               |       Appl. Authentication Code     3 Bytes
  1690.      +-             -+
  1691.  11  |               |
  1692.      +---------------+
  1693.  
  1694.      +===============+
  1695.      |               |
  1696.      |               |       Application Data              Data Sub-blocks
  1697.      |               |
  1698.      |               |
  1699.      +===============+
  1700.  
  1701.      +---------------+
  1702.   0  |               |       Block Terminator              Byte
  1703.      +---------------+
  1704.  
  1705.             i) Extension Introducer - Defines this block as an extension. This
  1706.             field contains the fixed value 0x21.
  1707.  
  1708.             ii) Application Extension Label - Identifies the block as an
  1709.             Application Extension. This field contains the fixed value 0xFF.
  1710.  
  1711.             iii) Block Size - Number of bytes in this extension block,
  1712.             following the Block Size field, up to but not including the
  1713.             beginning of the Application Data. This field contains the fixed
  1714.             value 11.
  1715.  
  1716.  
  1717.  
  1718.  
  1719.  
  1720.  
  1721.  
  1722.                                                                         23
  1723.  
  1724.  
  1725.             iv) Application Identifier - Sequence of eight printable ASCII
  1726.             characters used to identify the application owning the Application
  1727.             Extension.
  1728.  
  1729.             v) Application Authentication Code - Sequence of three bytes used
  1730.             to authenticate the Application Identifier. An Application program
  1731.             may use an algorithm to compute a binary code that uniquely
  1732.             identifies it as the application owning the Application Extension.
  1733.  
  1734.  
  1735.       d. Extensions and Scope. This block does not have scope. This block
  1736.       cannot be modified by any extension.
  1737.  
  1738.       e. Recommendation. None.
  1739.  
  1740.  
  1741. 27. Trailer.
  1742.  
  1743.       a. Description. This block is a single-field block indicating the end of
  1744.       the GIF Data Stream.  It contains the fixed value 0x3B.
  1745.  
  1746.       b. Required Version.  87a.
  1747.  
  1748.       c. Syntax.
  1749.  
  1750.       7 6 5 4 3 2 1 0        Field Name                    Type
  1751.      +---------------+
  1752.   0  |               |       GIF Trailer                   Byte
  1753.      +---------------+
  1754.  
  1755.       d. Extensions and Scope. This block does not have scope, it terminates
  1756.       the GIF Data Stream. This block may not be modified by any extension.
  1757.  
  1758.       e. Recommendations. None.
  1759.  
  1760.  
  1761.  
  1762.  
  1763.  
  1764.  
  1765.  
  1766.  
  1767.  
  1768.  
  1769.  
  1770.  
  1771.  
  1772.  
  1773.  
  1774.  
  1775.  
  1776.  
  1777.  
  1778.  
  1779.  
  1780.  
  1781.  
  1782.  
  1783.  
  1784.  
  1785.  
  1786.  
  1787.  
  1788.                                                                         24
  1789.  
  1790.  
  1791. Appendix
  1792. A. Quick Reference Table.
  1793.  
  1794. Block Name                  Required   Label       Ext.   Vers.
  1795. Application Extension       Opt. (*)   0xFF (255)  yes    89a
  1796. Comment Extension           Opt. (*)   0xFE (254)  yes    89a
  1797. Global Color Table          Opt. (1)   none        no     87a
  1798. Graphic Control Extension   Opt. (*)   0xF9 (249)  yes    89a
  1799. Header                      Req. (1)   none        no     N/A
  1800. Image Descriptor            Opt. (*)   0x2C (044)  no     87a (89a)
  1801. Local Color Table           Opt. (*)   none        no     87a
  1802. Logical Screen Descriptor   Req. (1)   none        no     87a (89a)
  1803. Plain Text Extension        Opt. (*)   0x01 (001)  yes    89a
  1804. Trailer                     Req. (1)   0x3B (059)  no     87a
  1805.  
  1806. Unlabeled Blocks
  1807. Header                      Req. (1)   none        no     N/A
  1808. Logical Screen Descriptor   Req. (1)   none        no     87a (89a)
  1809. Global Color Table          Opt. (1)   none        no     87a
  1810. Local Color Table           Opt. (*)   none        no     87a
  1811.  
  1812. Graphic-Rendering Blocks
  1813. Plain Text Extension        Opt. (*)   0x01 (001)  yes    89a
  1814. Image Descriptor            Opt. (*)   0x2C (044)  no     87a (89a)
  1815.  
  1816. Control Blocks
  1817. Graphic Control Extension   Opt. (*)   0xF9 (249)  yes    89a
  1818.  
  1819. Special Purpose Blocks
  1820. Trailer                     Req. (1)   0x3B (059)  no     87a
  1821. Comment Extension           Opt. (*)   0xFE (254)  yes    89a
  1822. Application Extension       Opt. (*)   0xFF (255)  yes    89a
  1823.  
  1824. legend:           (1)   if present, at most one occurrence
  1825.                   (*)   zero or more occurrences
  1826.                   (+)   one or more occurrences
  1827.  
  1828. Notes : The Header is not subject to Version Numbers.
  1829. (89a) The Logical Screen Descriptor and the Image Descriptor retained their
  1830. syntax from version 87a to version 89a, but some fields reserved under version
  1831. 87a are used under version 89a.
  1832.  
  1833.  
  1834.  
  1835.  
  1836.  
  1837.  
  1838.  
  1839.  
  1840.  
  1841.  
  1842.  
  1843.  
  1844.  
  1845.  
  1846.  
  1847.  
  1848.  
  1849.  
  1850.  
  1851.  
  1852.  
  1853.  
  1854.                                                                         25
  1855.  
  1856.  
  1857. Appendix
  1858. B. GIF Grammar.
  1859.  
  1860. A Grammar is a form of notation to represent the sequence in which certain
  1861. objects form larger objects.  A grammar is also used to represent the number of
  1862. objects that can occur at a given position.  The grammar given here represents
  1863. the sequence of blocks that form the GIF Data Stream. A grammar is given by
  1864. listing its rules.  Each rule consists of the left-hand side, followed by some
  1865. form of equals sign, followed by the right-hand side.  In a rule, the
  1866. right-hand side describes how the left-hand side is defined. The right-hand
  1867. side consists of a sequence of entities, with the possible presence of special
  1868. symbols. The following legend defines the symbols used in this grammar for GIF.
  1869.  
  1870. Legend:           <>    grammar word
  1871.                   ::=   defines symbol
  1872.                   *     zero or more occurrences
  1873.                   +     one or more occurrences
  1874.                   |     alternate element
  1875.                   []    optional element
  1876.  
  1877. Example:
  1878.  
  1879. <GIF Data Stream> ::= Header <Logical Screen> <Data>* Trailer
  1880.  
  1881. This rule defines the entity <GIF Data Stream> as follows. It must begin with a
  1882. Header. The Header is followed by an entity called Logical Screen, which is
  1883. defined below by another rule. The Logical Screen is followed by the entity
  1884. Data, which is also defined below by another rule. Finally, the entity Data is
  1885. followed by the Trailer.  Since there is no rule defining the Header or the
  1886. Trailer, this means that these blocks are defined in the document.  The entity
  1887. Data has a special symbol (*) following it which means that, at this position,
  1888. the entity Data may be repeated any number of times, including 0 times. For
  1889. further reading on this subject, refer to a standard text on Programming
  1890. Languages.
  1891.  
  1892.  
  1893. The Grammar.
  1894.  
  1895. <GIF Data Stream> ::=     Header <Logical Screen> <Data>* Trailer
  1896.  
  1897. <Logical Screen> ::=      Logical Screen Descriptor [Global Color Table]
  1898.  
  1899. <Data> ::=                <Graphic Block>  |
  1900.                           <Special-Purpose Block>
  1901.  
  1902. <Graphic Block> ::=       [Graphic Control Extension] <Graphic-Rendering Block>
  1903.  
  1904. <Graphic-Rendering Block> ::=  <Table-Based Image>  |
  1905.                                Plain Text Extension
  1906.  
  1907. <Table-Based Image> ::=   Image Descriptor [Local Color Table] Image Data
  1908.  
  1909. <Special-Purpose Block> ::=    Application Extension  |
  1910.                                Comment Extension
  1911.  
  1912.  
  1913.  
  1914.  
  1915.  
  1916.  
  1917.  
  1918.  
  1919.  
  1920.                                                                         26
  1921.  
  1922.  
  1923. NOTE : The grammar indicates that it is possible for a GIF Data Stream to
  1924. contain the Header, the Logical Screen Descriptor, a Global Color Table and the
  1925. GIF Trailer. This special case is used to load a GIF decoder with a Global
  1926. Color Table, in preparation for subsequent Data Streams without color tables at
  1927. all.
  1928.  
  1929.  
  1930.  
  1931.  
  1932.  
  1933.  
  1934.  
  1935.  
  1936.  
  1937.  
  1938.  
  1939.  
  1940.  
  1941.  
  1942.  
  1943.  
  1944.  
  1945.  
  1946.  
  1947.  
  1948.  
  1949.  
  1950.  
  1951.  
  1952.  
  1953.  
  1954.  
  1955.  
  1956.  
  1957.  
  1958.  
  1959.  
  1960.  
  1961.  
  1962.  
  1963.  
  1964.  
  1965.  
  1966.  
  1967.  
  1968.  
  1969.  
  1970.  
  1971.  
  1972.  
  1973.  
  1974.  
  1975.  
  1976.  
  1977.  
  1978.  
  1979.  
  1980.  
  1981.  
  1982.  
  1983.  
  1984.  
  1985.  
  1986.                                                                         27
  1987.  
  1988.  
  1989. Appendix
  1990. C. Glossary.
  1991.  
  1992. Active Color Table - Color table used to render the next graphic. If the next
  1993. graphic is an image which has a Local Color Table associated with it, the
  1994. active color table becomes the Local Color Table associated with that image.
  1995. If the next graphic is an image without a Local Color Table, or a Plain Text
  1996. Extension, the active color table is the Global Color Table associated with the
  1997. Data Stream, if there is one; if there is no Global Color Table in the Data
  1998. Stream, the active color table is a color table saved from a previous Data
  1999. Stream, or one supplied by the decoder.
  2000.  
  2001. Block - Collection of bytes forming a protocol unit. In general, the term
  2002. includes labeled and unlabeled blocks, as well as Extensions.
  2003.  
  2004. Data Stream - The GIF Data Stream is composed of blocks and sub-blocks
  2005. representing images and graphics, together with control information to render
  2006. them on a display device. All control and data blocks in the Data Stream must
  2007. follow the Header and must precede the Trailer.
  2008.  
  2009. Decoder - A program capable of processing a GIF Data Stream to render the
  2010. images and graphics contained in it.
  2011.  
  2012. Encoder - A program capable of capturing and formatting image and graphic
  2013. raster data, following the definitions of the Graphics Interchange Format.
  2014.  
  2015. Extension - A protocol block labeled by the Extension Introducer 0x21.
  2016.  
  2017. Extension Introducer - Label (0x21) defining an Extension.
  2018.  
  2019. Graphic - Data which can be rendered on the screen by virtue of some algorithm.
  2020. The term graphic is more general than the term image; in addition to images,
  2021. the term graphic also includes data such as text, which is rendered using
  2022. character bit-maps.
  2023.  
  2024. Image - Data representing a picture or a drawing; an image is represented by an
  2025. array of pixels called the raster of the image.
  2026.  
  2027. Raster - Array of pixel values representing an image.
  2028.  
  2029.  
  2030.  
  2031.  
  2032.  
  2033.  
  2034.  
  2035.  
  2036.  
  2037.  
  2038.  
  2039.  
  2040.  
  2041.  
  2042.  
  2043.  
  2044.  
  2045.  
  2046.  
  2047.  
  2048.  
  2049.  
  2050.  
  2051.  
  2052.                                                                         28
  2053.  
  2054.  
  2055. Appendix
  2056. D. Conventions.
  2057.  
  2058. Animation - The Graphics Interchange Format is not intended as a platform for
  2059. animation, even though it can be done in a limited way.
  2060.  
  2061. Byte Ordering - Unless otherwise stated, multi-byte numeric fields are ordered
  2062. with the Least Significant Byte first.
  2063.  
  2064. Color Indices - Color indices always refer to the active color table, either
  2065. the Global Color Table or the Local Color Table.
  2066.  
  2067. Color Order - Unless otherwise stated, all triple-component RGB color values
  2068. are specified in Red-Green-Blue order.
  2069.  
  2070. Color Tables - Both color tables, the Global and the Local, are optional; if
  2071. present, the Global Color Table is to be used with every image in the Data
  2072. Stream for which a Local Color Table is not given; if present, a Local Color
  2073. Table overrides the Global Color Table.  However, if neither color table is
  2074. present, the application program is free to use an arbitrary color table. If
  2075. the graphics in several Data Streams are related and all use the same color
  2076. table, an encoder could place the color table as the Global Color Table in the
  2077. first Data Stream and leave subsequent Data Streams without a Global Color
  2078. Table or any Local Color Tables; in this way, the overhead for the table is
  2079. eliminated.  It is recommended that the decoder save the previous Global Color
  2080. Table to be used with the Data Stream that follows, in case it does not contain
  2081. either a Global Color Table or any Local Color Tables. In general, this allows
  2082. the application program to use past color tables, significantly reducing
  2083. transmission overhead.
  2084.  
  2085. Extension Blocks - Extensions are defined using the Extension Introducer code
  2086. to mark the beginning of the block, followed by a block label, identifying the
  2087. type of extension.  Extension Codes are numbers in the range from 0x00 to 0xFF,
  2088. inclusive. Special purpose extensions are transparent to the decoder and may be
  2089. omitted when transmitting the Data Stream on-line. The GIF capabilities
  2090. dialogue makes the provision for the receiver to request the transmission of
  2091. all blocks; the default state in this regard is no transmission of Special
  2092. purpose blocks.
  2093.  
  2094. Reserved Fields - All Reserved Fields are expected to have each bit set to zero
  2095. (off).
  2096.  
  2097.  
  2098.  
  2099.  
  2100.  
  2101.  
  2102.  
  2103.  
  2104.  
  2105.  
  2106.  
  2107.  
  2108.  
  2109.  
  2110.  
  2111.  
  2112.  
  2113.  
  2114.  
  2115.  
  2116.  
  2117.  
  2118.                                                                         29
  2119.  
  2120.  
  2121. Appendix
  2122. E. Interlaced Images.
  2123.  
  2124. The rows of an Interlaced images are arranged in the following order:
  2125.  
  2126.       Group 1 : Every 8th. row, starting with row 0.              (Pass 1)
  2127.       Group 2 : Every 8th. row, starting with row 4.              (Pass 2)
  2128.       Group 3 : Every 4th. row, starting with row 2.              (Pass 3)
  2129.       Group 4 : Every 2nd. row, starting with row 1.              (Pass 4)
  2130.  
  2131. The Following example illustrates how the rows of an interlaced image are
  2132. ordered.
  2133.  
  2134.       Row Number                                        Interlace Pass
  2135.  
  2136.  0    -----------------------------------------       1
  2137.  1    -----------------------------------------                         4
  2138.  2    -----------------------------------------                   3
  2139.  3    -----------------------------------------                         4
  2140.  4    -----------------------------------------             2
  2141.  5    -----------------------------------------                         4
  2142.  6    -----------------------------------------                   3
  2143.  7    -----------------------------------------                         4
  2144.  8    -----------------------------------------       1
  2145.  9    -----------------------------------------                         4
  2146.  10   -----------------------------------------                   3
  2147.  11   -----------------------------------------                         4
  2148.  12   -----------------------------------------             2
  2149.  13   -----------------------------------------                         4
  2150.  14   -----------------------------------------                   3
  2151.  15   -----------------------------------------                         4
  2152.  16   -----------------------------------------       1
  2153.  17   -----------------------------------------                         4
  2154.  18   -----------------------------------------                   3
  2155.  19   -----------------------------------------                         4
  2156.  
  2157.  
  2158.  
  2159.  
  2160.  
  2161.  
  2162.  
  2163.  
  2164.  
  2165.  
  2166.  
  2167.  
  2168.  
  2169.  
  2170.  
  2171.  
  2172.  
  2173.  
  2174.  
  2175.  
  2176.  
  2177.  
  2178.  
  2179.  
  2180.  
  2181.  
  2182.  
  2183.  
  2184.                                                                         30
  2185.  
  2186.  
  2187. Appendix
  2188. F. Variable-Length-Code LZW Compression.
  2189.  
  2190. The Variable-Length-Code LZW Compression is a variation of the Lempel-Ziv
  2191. Compression algorithm in which variable-length codes are used to replace
  2192. patterns detected in the original data. The algorithm uses a code or
  2193. translation table constructed from the patterns encountered in the original
  2194. data; each new pattern is entered into the table and its index is used to
  2195. replace it in the compressed stream.
  2196.  
  2197. The compressor takes the data from the input stream and builds a code or
  2198. translation table with the patterns as it encounters them; each new pattern is
  2199. entered into the code table and its index is added to the output stream; when a
  2200. pattern is encountered which had been detected since the last code table
  2201. refresh, its index from the code table is put on the output stream, thus
  2202. achieving the data compression.  The expander takes input from the compressed
  2203. data stream and builds the code or translation table from it; as the compressed
  2204. data stream is processed, codes are used to index into the code table and the
  2205. corresponding data is put on the decompressed output stream, thus achieving
  2206. data decompression.  The details of the algorithm are explained below.  The
  2207. Variable-Length-Code aspect of the algorithm is based on an initial code size
  2208. (LZW-initial code size), which specifies the initial number of bits used for
  2209. the compression codes.  When the number of patterns detected by the compressor
  2210. in the input stream exceeds the number of patterns encodable with the current
  2211. number of bits, the number of bits per LZW code is increased by one.
  2212.  
  2213. The Raster Data stream that represents the actual output image can be
  2214. represented as:
  2215.  
  2216.          7 6 5 4 3 2 1 0
  2217.         +---------------+
  2218.         | LZW code size |
  2219.         +---------------+
  2220.  
  2221.         +---------------+ ----+
  2222.         |  block size   |     |
  2223.         +---------------+     |
  2224.         |               |     +-- Repeated as many
  2225.         |  data bytes   |     |   times as necessary.
  2226.         |               |     |
  2227.         +---------------+ ----+
  2228.  
  2229.         . . .       . . . ------- The code that terminates the LZW
  2230.                                   compressed data must appear before
  2231.                                   Block Terminator.
  2232.         +---------------+
  2233.         |0 0 0 0 0 0 0 0|  Block Terminator
  2234.         +---------------+
  2235.  
  2236. The conversion of the image from a series of pixel values to a transmitted or
  2237. stored character stream involves several steps. In brief these steps are:
  2238.  
  2239. 1. Establish the Code Size - Define the number of bits needed to represent the
  2240. actual data.
  2241.  
  2242. 2. Compress the Data - Compress the series of image pixels to a series of
  2243.  
  2244.  
  2245.  
  2246.  
  2247.  
  2248.  
  2249.  
  2250.                                                                         31
  2251.  
  2252.  
  2253. compression codes.
  2254.  
  2255. 3. Build a Series of Bytes - Take the set of compression codes and convert to a
  2256. string of 8-bit bytes.
  2257.  
  2258. 4. Package the Bytes - Package sets of bytes into blocks preceded by character
  2259. counts and output.
  2260.  
  2261. ESTABLISH CODE SIZE
  2262.  
  2263. The first byte of the Compressed Data stream is a value indicating the minimum
  2264. number of bits required to represent the set of actual pixel values. Normally
  2265. this will be the same as the number of color bits. Because of some algorithmic
  2266. constraints however, black & white images which have one color bit must be
  2267. indicated as having a code size of 2.
  2268. This code size value also implies that the compression codes must start out one
  2269. bit longer.
  2270.  
  2271. COMPRESSION
  2272.  
  2273. The LZW algorithm converts a series of data values into a series of codes which
  2274. may be raw values or a code designating a series of values. Using text
  2275. characters as an analogy, the output code consists of a character or a code
  2276. representing a string of characters.
  2277.  
  2278. The LZW algorithm used in GIF matches algorithmically with the standard LZW
  2279. algorithm with the following differences:
  2280.  
  2281. 1.  A special Clear code is defined which resets all compression/decompression
  2282. parameters and tables to a start-up state. The value of this code is 2**<code
  2283. size>. For example if the code size indicated was 4 (image was 4 bits/pixel)
  2284. the Clear code value would be 16 (10000 binary). The Clear code can appear at
  2285. any point in the image data stream and therefore requires the LZW algorithm to
  2286. process succeeding codes as if a new data stream was starting. Encoders should
  2287. output a Clear code as the first code of each image data stream.
  2288.  
  2289. 2. An End of Information code is defined that explicitly indicates the end of
  2290. the image data stream. LZW processing terminates when this code is encountered.
  2291. It must be the last code output by the encoder for an image. The value of this
  2292. code is <Clear code>+1.
  2293.  
  2294. 3. The first available compression code value is <Clear code>+2.
  2295.  
  2296. 4. The output codes are of variable length, starting at <code size>+1 bits per
  2297. code, up to 12 bits per code. This defines a maximum code value of 4095
  2298. (0xFFF). Whenever the LZW code value would exceed the current code length, the
  2299. code length is increased by one. The packing/unpacking of these codes must then
  2300. be altered to reflect the new code length.
  2301.  
  2302. BUILD 8-BIT BYTES
  2303.  
  2304. Because the LZW compression used for GIF creates a series of variable length
  2305. codes, of between 3 and 12 bits each, these codes must be reformed into a
  2306. series of 8-bit bytes that will be the characters actually stored or
  2307. transmitted. This provides additional compression of the image. The codes are
  2308. formed into a stream of bits as if they were packed right to left and then
  2309.  
  2310.  
  2311.  
  2312.  
  2313.  
  2314.  
  2315.  
  2316.                                                                         32
  2317.  
  2318.  
  2319. picked off 8 bits at a time to be output.
  2320.  
  2321. Assuming a character array of 8 bits per character and using 5 bit codes to be
  2322. packed, an example layout would be similar to:
  2323.  
  2324.  
  2325.      +---------------+
  2326.   0  |               |    bbbaaaaa
  2327.      +---------------+
  2328.   1  |               |    dcccccbb
  2329.      +---------------+
  2330.   2  |               |    eeeedddd
  2331.      +---------------+
  2332.   3  |               |    ggfffffe
  2333.      +---------------+
  2334.   4  |               |    hhhhhggg
  2335.      +---------------+
  2336.            . . .
  2337.      +---------------+
  2338.   N  |               |
  2339.      +---------------+
  2340.  
  2341.  
  2342. Note that the physical packing arrangement will change as the number of bits
  2343. per compression code change but the concept remains the same.
  2344.  
  2345. PACKAGE THE BYTES
  2346.  
  2347. Once the bytes have been created, they are grouped into blocks for output by
  2348. preceding each block of 0 to 255 bytes with a character count byte. A block
  2349. with a zero byte count terminates the Raster Data stream for a given image.
  2350. These blocks are what are actually output for the GIF image. This block format
  2351. has the side effect of allowing a decoding program the ability to read past the
  2352. actual image data if necessary by reading block counts and then skipping over
  2353. the data.
  2354.  
  2355.  
  2356.  
  2357. FURTHER READING
  2358.  
  2359. [1] Ziv, J. and Lempel, A. : "A Universal Algorithm for Sequential Data
  2360. Compression", IEEE Transactions on Information Theory, May 1977.
  2361. [2] Welch, T. : "A Technique for High-Performance Data Compression", Computer,
  2362. June 1984.
  2363. [3] Nelson, M.R. : "LZW Data Compression", Dr. Dobb's Journal, October 1989.
  2364.  
  2365.  
  2366.  
  2367.  
  2368.  
  2369.  
  2370.  
  2371.  
  2372.  
  2373.  
  2374.  
  2375.  
  2376.  
  2377.  
  2378.  
  2379.  
  2380.  
  2381.  
  2382.                                                                         33
  2383.  
  2384.  
  2385. Appendix
  2386. G. On-line Capabilities Dialogue.
  2387.  
  2388. NOTE : This section is currently (10 July 1990) under revision; the information
  2389. provided here should be used as general guidelines. Code written based on this
  2390. information should be designed in a flexible way to accommodate any changes
  2391. resulting from the revisions.
  2392.  
  2393. The following sequences are defined for use in mediating control between a GIF
  2394. sender and GIF receiver over an interactive communications line. These
  2395. sequences do not apply to applications that involve downloading of static GIF
  2396. files and are not considered part of a GIF file.
  2397.  
  2398. GIF CAPABILITIES ENQUIRY
  2399.  
  2400. The GIF Capabilities Enquiry sequence is issued from a host and requests an
  2401. interactive GIF decoder to return a response message that defines the graphics
  2402. parameters for the decoder. This involves returning information about available
  2403. screen sizes, number of bits/color supported and the amount of color detail
  2404. supported. The escape sequence for the GIF Capabilities Enquiry is defined as:
  2405.  
  2406. ESC[>0g           0x1B 0x5B 0x3E 0x30 0x67
  2407.  
  2408. GIF CAPABILITIES RESPONSE
  2409.  
  2410. The GIF Capabilities Response message is returned by an interactive GIF decoder
  2411. and defines the decoder's display capabilities for all graphics modes that are
  2412. supported by the software. Note that this can also include graphics printers as
  2413. well as a monitor screen. The general format of this message is:
  2414.  
  2415. #version;protocol{;dev, width, height, color-bits, color-res}...<CR>
  2416.  
  2417.  
  2418. '#'            GIF Capabilities Response identifier character.
  2419. version        GIF format version number;  initially '87a'.
  2420. protocol='0'   No end-to-end protocol supported by decoder Transfer as direct
  2421.                8-bit data stream.
  2422. protocol='1'   Can use CIS B+ error correction protocol to transfer GIF data
  2423.                interactively from the host directly to the display.
  2424. dev = '0'      Screen parameter set follows.
  2425. dev = '1'      Printer parameter set follows.
  2426. width          Maximum supported display width in pixels.
  2427. height         Maximum supported display height in pixels.
  2428. color-bits     Number of bits per pixel supported. The number of supported
  2429.                colors is therefore 2**color-bits.
  2430. color-res      Number of bits per color component supported in the hardware
  2431.                color palette. If color-res is '0' then no hardware palette
  2432.                table is available.
  2433.  
  2434. Note that all values in the GIF Capabilities Response are returned as ASCII
  2435. decimal numbers and the message is terminated by a Carriage Return character.
  2436.  
  2437. The following GIF Capabilities Response message describes three standard IBM PC
  2438. Enhanced Graphics Adapter configurations with no printer; the GIF data stream
  2439.  
  2440.  
  2441.  
  2442.  
  2443.  
  2444.  
  2445.  
  2446.  
  2447.  
  2448.                                                                         34
  2449.  
  2450.  
  2451. can be processed within an error correcting protocol:
  2452.  
  2453. #87a;1;0,320,200,4,0;0,640,200,2,2;0,640,350,4,2<CR>
  2454.  
  2455. ENTER GIF GRAPHICS MODE
  2456.  
  2457. Two sequences are currently defined to invoke an interactive GIF decoder into
  2458. action. The only difference between them is that different output media are
  2459. selected. These sequences are:
  2460.  
  2461. ESC[>1g     Display GIF image on screen
  2462.  
  2463.                   0x1B 0x5B 0x3E 0x31 0x67
  2464.  
  2465. ESC[>2g   Display image directly to an attached graphics printer. The image may
  2466. optionally be displayed on the screen as well.
  2467.  
  2468.                   0x1B 0x5B 0x3E 0x32 0x67
  2469.  
  2470. Note that the 'g' character terminating each sequence is in lowercase.
  2471.  
  2472. INTERACTIVE ENVIRONMENT
  2473.  
  2474. The assumed environment for the transmission of GIF image data from an
  2475. interactive application is a full 8-bit data stream from host to micro.  All
  2476. 256 character codes must be transferrable. The establishing of an 8-bit data
  2477. path for communications will normally be taken care of by the host application
  2478. programs. It is however up to the receiving communications programs supporting
  2479. GIF to be able to receive and pass on all 256 8-bit codes to the GIF decoder
  2480. software.
  2481.